﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Xml.Linq;

namespace UavForm
{
    public partial class Home: Form
    {
        public DbHelper dbHelper;

        public Home()
        {
            InitializeComponent();
        }
        private void Home_Load(object sender, EventArgs e)
        {
            dbHelper = new DbHelper();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Filter = "All files (*.*)|*.*"
            };

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog.FileName;
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            try
            {
                var xmlFile = textBox1.Text;
                var doc = XDocument.Load(xmlFile);

                foreach (var node in doc.Root.Elements())
                {
                    if (node.Name.LocalName.StartsWith("recording"))
                    {
                        var data = new UavModel
                        {
                            Day = int.Parse(node.Element("Day").Value),
                            RunningTime = int.Parse(node.Element("RunningTime").Value),
                            Speed = double.Parse(node.Element("Speed").Value),
                            CabTemp = double.Parse(node.Element("CabTemp").Value),
                            EngTemp = double.Parse(node.Element("EngTemp").Value),
                            Fuel = double.Parse(node.Element("Fuel").Value),
                            Battery = double.Parse(node.Element("Battery").Value),
                            ShockWear = double.Parse(node.Element("ShockWear").Value)
                        };
                        dbHelper.AddFlightData(data);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Data import failed: {ex.Message}");
                return;
            }
            MessageBox.Show("Data saved success");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //this.Hide();
            if (radioButton1.Checked)
            {
                Normal nor = new Normal();
                nor.ShowDialog();
            }
            else
            {
                Professional pro = new Professional();
                pro.ShowDialog();
            }
            //this.Show();
        }
    }
}
